<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		protected $fang;
		protected $config;
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			parent::__construct();
			$this->getConfig();
			set_time_limit(0);
		}
		/**
		 * 获取配置项
		 * 
		 */
		public function getConfig(){
			$sql = "select * from yang_config";
			$config = $this->select($sql);
			$configLast = [];
			foreach($config as $key=>$value){
				$configLast[$value['key']] = $value['value'];
			}
			$this->config = $configLast;
		}
		//开始自动程序
		public function run(){
			$profitsql="update yang_profit set jj_num = 0 , sc_num = 0 , fx_num = 0 , sq_num = 0 , point_num = 0 , global_num = 0";
			$r=$this->query($profitsql);
			dump($r);
			$project_dayArr=[];
			$project_profitArr=[];
			$jt_arr=[];
			$suocang_Arr=[];
			$fx_Arr=[];
			$sq_Arr=[];
			$jt_arr2=[];
			$suocang_Arr2=[];
			$fx_Arr2=[];
			$sq_Arr2=[];
			$financeArr=[];
			$sql="select member_id ,pid,username from yang_member";
			$list=$this->select($sql);
			$sql="select * from yang_price";
			$pricearr=$this->find($sql);
			foreach($list as $k=>$v){
				$tid=$v['member_id']%10;
				$sql="select id from yang_member_money where currency_id = 5 and uid =".$v['member_id'];
				$par=$this->find($sql);
				//基金收益
				$sql="select * from yang_member_money where (num_lizi!=0 or num_suocang!=0) and uid =".$v['member_id'];
				$moneylist=$this->select($sql);
				if(!$moneylist){
					continue;
				}
				$yeji=0;
				foreach($moneylist as $k1=>$v1){
					$act_num=$v1['num_lizi']+$v1['num_suocang'];
					$yeji+=$act_num*$pricearr['price'.$v1['currency_id']];
				}
				$jt_num=($yeji*$this->config['day_bili'])/$pricearr['price5'];
				$jt_arr[] = [
						'id'=>$par['id'],
						'num'=>'num+'.$jt_num,
					];
				$jt_arr2[] = [
						'uid'=>$v['member_id'],
						'jj_num'=>'jj_num+'.$jt_num,
					];
				$financeArr[$tid][] = [
						'finance_id'=>'',
						'member_id'=>$v['member_id'],
						'type'=>1,
						'content'=>'基金收益',
						'money_type'=>1,
						'money'=>$jt_num,
						'add_time'=>time(),
						'currency_id'=>5,
						'ip'=>'127.0.0.1'
					];
				//锁仓收益
				$sql="select * from yang_project where status = 0 and uid =".$v['member_id'];
				$projectlist=$this->select($sql);
				$project_num=0;
				foreach($projectlist as $k2=>$v2){
					$project_num+=$v2['num']*$v2['bili']*$pricearr['price'.$v2['currency_id']];
					$project_dayArr[] = [
						'id'=>$v2['id'],
						'deal_day'=>'deal_day+1',
					];
					$project_profitArr[] = [
						'id'=>$v2['id'],
						'profit'=>'profit+'.($v2['num']*$v2['bili']),
					];
				}
				$suocang_num=$project_num/$pricearr['price5'];
				$suocang_Arr[] = [
						'id'=>$par['id'],
						'num'=>'num+'.$suocang_num,
					];
				$suocang_Arr2[] = [
						'uid'=>$v['member_id'],
						'sc_num'=>'sc_num+'.$suocang_num,
					];
				$financeArr[$tid][] = [
						'finance_id'=>'',
						'member_id'=>$v['member_id'],
						'type'=>1,
						'content'=>'锁仓收益',
						'money_type'=>1,
						'money'=>$suocang_num,
						'add_time'=>time(),
						'currency_id'=>5,
						'ip'=>'127.0.0.1'
					];
				//分享收益
				$zt_num=$jt_num+$suocang_num;
				$pidarr=$this->runZT($v['member_id'],1);
				foreach($pidarr as $k3=>$v3){
					$sql="select p_num from yang_user_relation where uid =".$v3['uid'];
					$p_num=$this->find($sql)['p_num'];
					$sql="select id from yang_member_money where currency_id = 5 and uid =".$v3['uid'];
					$par=$this->find($sql);
					if($k3<=14){						
						$tid=$v3['uid']%10;
						$bili=0;
						if($k3==0){
							if($p_num==1){
								$bili=$this->config['daishu1_1'];
							}else if($p_num==2){
								$bili=$this->config['daishu1_2'];
							}else if($p_num>=3){
								$bili=$this->config['daishu1_3'];
							}
						}else{
							if($p_num>=$k3){
								if($k3==1){
									$bili=$this->config['daishu2'];
								}else if($k3==2){
									$bili=$this->config['daishu3'];
								}else if($k3>=3&&$k3<=9){
									$bili=$this->config['daishu4'];
								}else if($k3>=10&&$k3<=14){
									$bili=$this->config['daishu5'];
								}
							}
						}
						if($bili!=0){
							$zt_reward=$zt_num*$bili;
							$fx_Arr[] = [
								'id'=>$par['id'],
								'num'=>'num+'.$zt_reward,
							];
							$fx_Arr2[] = [
								'uid'=>$v3['uid'],
								'fx_num'=>'fx_num+'.$zt_reward,
							];
							$financeArr[$tid][] = [
								'finance_id'=>'',
								'member_id'=>$v3['uid'],
								'type'=>1,
								'content'=>'来自'.$v['username'].'的分享收益',
								'money_type'=>1,
								'money'=>$zt_reward,
								'add_time'=>time(),
								'currency_id'=>5,
								'ip'=>'127.0.0.1'
							];
						}
					}
					$sq_num=$zt_num*$this->config['sq_bili'.$v3['level']];
						$sq_Arr[] = [
							'id'=>$par['id'],
							'num'=>'num+'.$sq_num,
						];
						$sq_Arr2[] = [
							'uid'=>$v3['uid'],
							'sq_num'=>'sq_num+'.$sq_num,
						];
						$financeArr[$tid][] = [
							'finance_id'=>'',
							'member_id'=>$v3['uid'],
							'type'=>1,
							'content'=>'来自'.$v['username'].'的社区收益',
							'money_type'=>1,
							'money'=>$act_num,
							'add_time'=>time(),
							'currency_id'=>5,
							'ip'=>'127.0.0.1'
						];					
				}									
				}
				$point_Arr=[];
				$point_Arr2=[];
				$point_Arr3=[];
				//节点收益
				$sql="select * from yang_point_log ";
				$point=$this->select($sql);
				foreach($point as $k=>$v){
					if($v['profit']<$v['num']){
						$tid=$v['uid']%10;
						$sql="select id from yang_member_money where currency_id = 5 and uid =".$v['uid'];
						$par=$this->find($sql);
						$num=$v['num']*$v['bili'];
						$par_num=($num*$pricearr['price'.$v['currency_id']])/$pricearr['price5'];
						$point_Arr[] = [
							'id'=>$v['id'],
							'profit'=>'profit+'.$num,
						];
						$point_Arr2[] = [
							'id'=>$par['id'],
							'num'=>'num+'.$par_num,
						];
						$point_Arr3[] = [
							'uid'=>$v['uid'],
							'point_num'=>'point_num+'.$par_num,
						];
						$financeArr[$tid][] = [
							'finance_id'=>'',
							'member_id'=>$v['uid'],
							'type'=>1,
							'content'=>'超级节点收益',
							'money_type'=>1,
							'money'=>$par_num,
							'add_time'=>time(),
							'currency_id'=>5,
							'ip'=>'127.0.0.1'
						];		
					}
				}
				if(!empty($financeArr)){
					foreach($financeArr as $k=>$v){
						$this->insert('yang_finance_'.$k, $v);
					}
				}
				if(!empty($point_Arr)){
					$this->update('yang_price_log', 'id', 'profit', $point_Arr);
				}
				if(!empty($point_Arr2)){
					$this->update('yang_member_money', 'id', 'num', $point_Arr);
				}
				if(!empty($point_Arr3)){
					$this->update('yang_profit', 'uid', 'point_num', $point_Arr);
				}
				if(!empty($project_dayArr)){
					$this->update('yang_project', 'id', 'deal_day', $project_dayArr);
				}
				if(!empty($project_profitArr)){
					$this->update('yang_project', 'id', 'profit', $project_profitArr);
				}
				if(!empty($jt_arr)){
					$this->update('yang_member_money', 'id', 'num', $jt_arr);
				}
				if(!empty($suocang_Arr)){
					$this->update('yang_member_money', 'id', 'num', $suocang_Arr);
				}
				if(!empty($fx_Arr)){
					$this->update('yang_member_money', 'id', 'num', $fx_Arr);
				}
				if(!empty($sq_Arr)){
					$this->update('yang_member_money', 'id', 'num', $sq_Arr);
				}
				if(!empty($jt_arr2)){
					$this->update('yang_profit', 'uid', 'jj_num', $jt_arr2);
				}
				if(!empty($suocang_Arr2)){
					$this->update('yang_profit', 'uid', 'sc_num', $suocang_Arr2);
				}
				if(!empty($fx_Arr2)){
					$this->update('yang_profit', 'uid', 'fx_num', $fx_Arr2);
				}
				if(!empty($sq_Arr2)){
					$this->update('yang_profit', 'uid', 'sq_num', $sq_Arr2);
				}
			}
			public function runZT($member_id,$l,&$upAll = []){
			$sql = "select * from yang_member where member_id = ".$member_id;
			$memberFind = $this->find($sql);
			if(!empty($memberFind['pid'])){
				$sql = "select * from yang_member where member_id = ".$memberFind['pid'];
				$memberUp =$this->find($sql);
				$upAll[] =[
					'uid'=>$memberFind['pid'],
					'level'=>$memberFind['level'],
				];
				$this->runZT($memberFind['pid'],$upAll);
			}
			return $upAll;
		}
		}		
	
	$auto=new auto();
	$auto->run();
	
?>